<?php
namespace Home\Controller;
use \Home\Model\DonateModel;
use \Home\Controller\SafeController;
include "./Common/CheckUser.php";

final class DonateController{
	
	//
	public $uid;
	public $name;
	public $qq;
	public $phone;
	
	//分页的参数
	public $pagesize;		//每页行数
	public $page;			//当前页数
	public $startrows;		//开始行号
	
	//对象
	public $donateModelObj;
	public $safeObj;
	
	public function __construct(){
		session_start();

		$this->uid = $_SESSION['uid'];
		$this->qq = $_SESSION['qq'];
		$this->name = $_SESSION['name'];
		$this->phone = $_SESSION['phone'];
		$this->pagesize = $GLOBALS['config']['pagesize'];		//每页行数
		
		$this->page = isset($_GET['page'])?$_GET['page']:1;		//当前页数
        if($_GET['page']==null){
            $this->page = 1;
        }
		$this->startrows = ($this->page-1)*$this->pagesize;					//开始行号
		$this->donateModelObj = new DonateModel();
		$this->safeObj = new SafeController();
	}
	
	//前台采用异步加载数据，所以尽量不要include了，直接返回Json即可
	
	//捐款记录列表视图
	public function donateMoneyList(){
        include VIEW_PATH."donate_money_list.html";
    }
	
	//捐书记录列表视图
	public function donateBookList(){
        include VIEW_PATH."donate_book_list.html";
    }
	
	
	//获取捐款记录列表
	public function getDonateMoneyList(){
		//获取某用户的捐款总记录数
		$rows = $this->donateModelObj->getMoneyRowsByUser($this->uid);

		//计算总页数、开始页码、结束页码的代码都在Common下
		include "./Common/page.php";
		
		//获取某用户的捐款记录结果集
		$arrs = $this->donateModelObj->getUserDonateMoneyList($this->startrows,$this->pagesize,$this->uid);
		//构造一个结果数组
		//包含请求状态、用户昵称、用户QQ、结果集
		$res = [
			'code' => 200,
			'name' => $this->name,
			'qq' => $this->qq,
			'page' => $this->page,
			'start' => $start,
			'end' => $end,
			'pages' => $pages,
			'res_arr' => $arrs
		];
		echo json_encode($res);
    }
	
	//获取捐书记录列表
	public function getDonateBookList(){
        //获取某用户的捐款总记录数
		$rows = $this->donateModelObj->getBookRowsByUser($this->uid);

		//计算总页数、开始页码、结束页码的代码都在Common下
		include "./Common/page.php";
		
		//获取某用户的捐款记录结果集
		$arrs = $this->donateModelObj->getUserDonateBookList($this->startrows,$this->pagesize,$this->uid);
		//构造一个结果数组
		//包含请求状态、用户昵称、用户QQ、结果集
		$res = [
			'code' => 200,
			'name' => $this->name,
			'qq' => $this->qq,
			'page' => $this->page,
			'start' => $start,
			'end' => $end,
			'pages' => $pages,
			'res_arr' => $arrs
		];
		echo json_encode($res);
    }
	

	//获取某用户某次捐书记录的详情数据
	public function getDonateBookDetail(){
		$id = $_POST['donate_book_id'];
		$arrs = $this->donateModelObj->getDonateBookDetail($id);
		//构造一个结果数组
		$res = [
			'code' => 200,
			'res_arr' => $arrs
		];
		echo json_encode($res);
	}
	//加载捐款视图
    public function donateMoneyView(){
        //生成trade_no
        $trade_no = date("YmdHis").mt_rand(100,999);
        include VIEW_PATH."donate_money.html";
    }
	
	//加载捐书视图(验证)——这个先不做了，先直接让用户可以捐赠即可
	//加载捐书视图(加书)
	public function donateBookView(){
        include VIEW_PATH."donate_book.html";
    }
	
	//添加捐书记录基本信息
	public function addDonateBook(){
		//第一步
		//这个方法用来接受用户填写的第一个表单的信息，包括自动填入的uid，用户希望展示的昵称、联系方式等
		$nickname = $_POST['nickname'];
		$contact = $_POST['contact'];
		//过滤
		$nickname = $this->safeObj->simpleClean($nickname);
		$contact = $this->safeObj->simpleClean($contact);
		//如果为空就使用用户的默认信息
		if($nickname==null || $nickname==''){
			$nickname = $this->name;
		}
		if($contact==null || $contact==''){
			$contact = $this->phone;
		}
		//插入love_donate_book并返回ID
		$donate_book_id = $this->donateModelObj->addDonateBook($this->uid,$nickname,$contact);
		if($donate_book_id!=0){
			$res = [
				'code' => 200,
				'mes' => '成功，请添加书籍',
				'donate_book_id' => $donate_book_id
			];
		}else{
			$res = [
				'code' => 500,
				'mes' => '失败，请重试'
			];
		}
		echo json_encode($res);
	}
	
	//每单本捐赠书籍的添加
	public function addDonateBookDetail(){
		//第二步
		//用这个方法为用户的某一次捐书记录在love_donate_book_detail中插入详细的书籍信息
		$donate_book_id = $_POST['donate_book_id'];
		$book_name = $_POST['book_name'];
		$book_count = $_POST['book_count'];
		$isbn = $_POST['isbn'];
		//将ISBN的横杠去掉
		$isbn =  preg_replace('/[-]/','', $isbn);
		if($donate_book_id==null){
			$res = [
				'code' => 500,
				'mes' => '系统开小差啦，请重新返回上一步重新提交信息'
			];
			echo json_encode($res);
			exit();
		}else if($book_name==null || $book_count==null || $isbn==null ){
			$res = [
				'code' => 500,
				'mes' => '你输入的书籍参数有空值，请检查'
			];
			echo json_encode($res);
			exit();
		}else if(!preg_match("/^(?=(?:\D*\d){10}(?:(?:\D*\d){3})?$)[\d-]+$/",$isbn)){
			$res = [
				'code' => 500,
				'mes' => '你输入的ISBN格式有误，请检查'
			];
			echo json_encode($res);
			exit();
		}
		$book_name = $this->safeObj->simpleClean($book_name);
		$book_count = $this->safeObj->simpleClean($book_count);
		$isbn = $this->safeObj->simpleClean($isbn);
		//插入
		$arr = $this->donateModelObj->addDonateBookDetail($donate_book_id,$book_name,$book_count,$isbn);
		if($arr['row']==1){
			$res = [
				'code' => 200,
				'mes' => '添加书籍成功',
				'donate_book_detail_id' => $arr['id']
			];
		}else{
			$res = [
				'code' => 500,
				'mes' => '服务器出错啦，请重试'
			];
		}
		echo json_encode($res);
	}
	
	//删除某条捐书明细
	public function delBookDetail(){
		$id = $_GET['donate_book_detail_id'];
		//查看该条记录所对应的记录ID
		//查看该记录的uid,和uid不匹配就不能删除
		//上面这里先不实现，等以后版本再实现
		//但是如果是已入库，就不能删除
		$res = $this->donateModelObj->getStatus($id);
		if($res['status']==1){
			echo 500;
			exit();
		}
		$row = $this->donateModelObj->delBookDetail($id);
		if($row==1){
			echo 200;
		}else{
			echo 500;
		}
	}
	
	//捐赠资金
	//说明，这里比较复杂，单纯靠控制器完成不了
	//这里发起后，靠epayapi.php初始化一系列的类，然后对数据进行处理、签名、向接口发起请求
	//最后的回调，异步在/Frame/Vendor/pay/notify_url.php
	//同步在/Frame/Vendor/pay/return_url.php
	//因此，用户支付成功后的展示信息和跳转代码，应该在/Frame/Vendor/pay/return_url.php去完成
	public function addDonateMoney(){
		$uid = $this->uid;
        $out_trade_no = $_POST['WIDout_trade_no'];
        //商户网站订单系统中唯一订单号，必填
		//支付方式
        $type = $_POST['type'];
        //（商品）名称
        $name = '博书公益爱心捐款';
		//付款金额
        $money = $_POST['WIDtotal_fee'];

        //存入记录到数据库，状态为未支付
        $row = $this->donateModelObj->addNewPay($uid,$out_trade_no,$money,$type);
        if($row==1){
        include $_SERVER['DOCUMENT_ROOT']."/Frame/Vendor/pay/epayapi.php";
        }else{
            echo "捐赠出错，请联系管理员！";
        }
	}
	
	//完成捐书后
	
	
}
?>